<template>
	<view class="wrap area-b" :style="{height:`${height}px`}">
		<u-navbar title="预览视频">
			<view class="u-m-r-30" slot="right">
				<text class="text-blue" @tap="save">保存</text>
			</view>
		</u-navbar>
		<video class="body" :src="src" :autoplay="true"></video>
	</view>
</template>

<script>
import { downloadFile } from '@/utils/file.js';
import { toast } from '@/utils/util';
	export default {
		data() {
			return {
				src:'',
				height:0
			};
		},
		onLoad({src}) {
			this.src = src
			this.height = uni.getSystemInfoSync().windowHeight
		},
		methods:{
			async save(){
				try{
					uni.showLoading({
						title:'下载文件中...'
					})
					const tempFilePath = await downloadFile({
						url:this.src
					})
					uni.showLoading({
						title:'保存文件中...'
					})
					await new Promise((resolve,reject)=>{
						uni.saveVideoToPhotosAlbum({
							filePath:tempFilePath,
							success:resolve,
							fail:reject
						})
					})
					uni.showToast({
						icon:"success",
						title:'保存成功'
					})
				}catch(e){
					console.log(e)
					toast(e.message||'保存失败')
				}
			}
		}
	}
</script>
<style lang="scss" scoped>
	.wrap{
		display: flex;
		flex-direction: column;
		width: 750rpx;
		.body{
			flex: 1;
			width: 100%;
		}
	}
</style>
